#!/usr/bin/env python
#
# Maestro is a music database search utility
# Copyright (C) 2010 Robert Mens
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

from __future__ import print_function
from optparse import OptionParser
import sys, os.path, os, codecs, string
import cli_interface, settings, musicdb

#decode our commandline arguments in unicode
unicode_argv = []
for arg in sys.argv:
    unicode_argv.append(arg.decode(sys.stdin.encoding))
sys.argv = unicode_argv
sys.stdout = codecs.getwriter(sys.stdin.encoding)(sys.stdout) # fixes pipes and redirection

settings.load()

parser = OptionParser(usage="usage: %prog [options] song")

parser.add_option("-x", "--search-only", help="Just search, don't play anything or even ask about it", action="store_true", dest="silent", default=False)
parser.add_option("-s", "--scan", help="Scan library", action="store_true", dest="scan", default=False)
parser.add_option("-e", "--enqueue", help="Don't play immediatly but add to the playlist", action="store_true", dest="enqueue", default=False)
parser.add_option("-v", "--verbose", help="Display more verbose information about the songs found", action="store_true", dest="verbose", default=False)
parser.add_option("-p", "--play", type="int", help="Play or enqueue a song by the result id", dest="play_id", default=0)
parser.add_option("-l", "--library", type="string", help="Use the given library file", dest="library", default="~/.maestro/maestro.db")
parser.add_option("-n", "--newdb", help="Create a fresh database", action="store_true", dest="newdb", default=False)
parser.add_option("-d", "--directory", type="string", help="Scan a specific directory", dest="scan_directory")
parser.add_option("-f", "--filename", help="Return the filename(s) of the result(s)", action="store_true", dest="filenames", default=False)
parser.add_option("-c", "--clean", help="Clean the database from missing files", action="store_true", dest="clean_db", default=False)

(options, args) = parser.parse_args()

database = os.path.abspath(os.path.expanduser(options.library))
interface = cli_interface.CliInterface()
settings.mdb = musicdb.MusicDB(os.path.expanduser(options.library))

if options.scan:
    import scanner

    #if options.append: print("Appending to current database")
    print("Using database: %s\n"%database)
    settings.mdb.close()
    
    if options.newdb:
        if os.path.exists(database): os.unlink(database)

    if not os.path.exists(database): options.newdb = True
    db = musicdb.MusicDB(database)

    if options.newdb: db.create_new_db()
    
    if options.scan_directory:
        scanner.scan_library(options.scan_directory, db)
    else:
        for directory in settings.directories:
            scanner.scan_library(directory, db)

elif options.clean_db:
    interface.clean_db()
    
else:
    if(len(args) > 0):
        interface.search_song(settings.mdb.merge_query(args), options.silent, True, options.enqueue, options.verbose, options.play_id, options.filenames)
    else:
        parser.print_help()
